//INCLUDES//
#include "tree.h"
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
//INCLUDES//



//INITIALISATION OF THE VARIABLES//
int node::node_id = 0;
int node::space = 0;
//INITIALISATION OF THE VARIABLES//



//CONSTRUCTOR//
node::node(const std::string& name1){
	node_id=node_id+1;
	if(name1 == "")
	{
		std::stringstream str_sm;
		str_sm <<"node_"<<node_id;
		std::string node_id_str = str_sm.str();
		name=node_id_str;
	}
	else
	name=name1;
	spacesPerNode=0;
}
//CONSTRUCTOR//



//DESTRUCTOR//
node::~node(){
	cout<<"enter ~node() of \""<<get_name()<<"\""<<endl;
	for(unsigned int i=0;i<child.size();i++){
		child[i]->~node();
	}
	cout<<"leave ~node() of \""<<get_name()<<"\""<<endl;
}
//DESTRUCTOR//



//GET_NAME//
const std::string node::get_name(){
	return this->name;
}
//GET_NAME//



//SET_NAME//
void node::set_name(std::string new_name){
	name=new_name;
}
//SET_NAME//



//GET_NR_CHILDREN//
const int node::get_nr_children(){
	return child.size();
}
//GET_NR_CHILDREN//



//GET_CHILD//
node* node::get_child(int i){
	int size = child.size();
	if(i>=size){
		cout<<get_name()<<" hat nicht so viele Kindknoten!"<<endl;
		return 0;
	}
		return child.at(i);
}
//GET_CHILD//



//ADD_CHILD//
void node::add_child(node *i){
	child.insert(child.end(), i);
	//TEST
	i->spacesPerNode = spacesPerNode+1;
}
//ADD_CHILD//



//COUNT_ALL_LOWERNODES//
const int node::countAllLowerNodes(){
	int p=child.size();
	if(p==0) return 0;
	for(unsigned int i=0;i<child.size();i++){
		p=p+child.at(i)->countAllLowerNodes();
	}
	return p;
}
//COUNT_ALL_LOWERNODES//



//CREATE_COMPLETE_TREE//
node* create_complete_tree(unsigned int nr_child_nodes, unsigned int tree_depth){
	node* temp = new node("");
	temp->spacesPerNode = temp->space;
	if(tree_depth >1)
		for(unsigned int i=0; i<nr_child_nodes; ++i) {
			temp->space += 1;
			temp->add_child(create_complete_tree(nr_child_nodes, tree_depth-1));
	}
	temp->space -= 1;
	return temp;
}
//CREATE_COMPLETE_TREE//



//ohne Zyklen
//OUTSTREAM_NODES//
ostream& operator <<(ostream& stream, node* n){
	for(int i=0;i<n->spacesPerNode;i++){
		stream<<"   ";
	}
	stream<<n->get_name()<<endl;
	for(int i=0;i<n->get_nr_children();i++){
		cout<<n->get_child(i);
	}
	return stream;
}
//OUTSTREAM_NODES//


//erkennt Zyklen (funktioniert noch nicht)
//OUTSTREAM_NODES//
/*ostream& operator <<(ostream& stream, node* n){
	n->add_node(n);
	for(int i=0;i<n->spacesPerNode;i++){
		stream<<"   ";
	}
	stream<<n->get_name()<<endl;
	for(int i=0;i<n->get_nr_children();i++){
		cout<<n->get_child(i);
	}
	
	return stream;
}*/
//OUTSTREAM_NODES//
